package com.gopay.bis.manager.bank.bcdl;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.constants.bank.PabConstants;
import com.gopay.common.domain.bank.BcdlMessage;
import com.gopay.common.domain.bank.pab.bcdl.PABOnlineHeaderMessage;
import com.gopay.common.domain.bank.pab.bcdl.req.ReqPAB4004;
import com.gopay.common.domain.bank.pab.bcdl.req.ReqPAB4005;
import com.gopay.common.domain.bank.pab.bcdl.res.ResPAB4004;
import com.gopay.common.domain.bank.pab.bcdl.res.ResPAB4005;
import com.gopay.common.domain.bis.BisBcdlIntrConfig;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.bank.businesses.BcdlService;

public class PabBcdlManager {

	private static Logger logger = LoggerFactory.getLogger(PabBcdlManager.class);		
	
	@Resource(name = "bosConfig")
	private Map<String, String> pabConfig;	
	@Resource(name = "pabBcdlService")
	private BcdlService pabBcdlService;

	public ResPAB4004 eft(BcdlMessage base)throws GopayException {
		BcdlMessage payReq = base;
		ReqPAB4004 req4004 = builder4004Req(payReq);
		ResPAB4004 res4004 = (ResPAB4004) pabBcdlService.eft(req4004);
		return res4004;
	}

	public ResPAB4005 query(BcdlMessage base)throws GopayException {
		BcdlMessage req = base;
		ReqPAB4005 req4005 = builder4005Req(req);
		ResPAB4005 res4005 = (ResPAB4005) pabBcdlService.query(req4005);
		logger.error("PAB-4005回应解析：" + res4005);
		return res4005;
	}

	/**
	 * 
	 * @param req
	 * @return
	 */
	private ReqPAB4005 builder4005Req(BcdlMessage req)throws GopayException {
		PABOnlineHeaderMessage header = builderSDBOnlineHeaderMessage(PabConstants.ONLINE_TXN_TYPE_4005,((BisBcdlIntrConfig) req.getBankIntrChannConf()).getIntrMernum1());
		ReqPAB4005 req4005 = new ReqPAB4005();
		req4005.setOrigThirdVoucher(req.getOutOrderId());
		req4005.setHeader(header);
		return req4005;
	}

	/**
	 * 
	 * @param req
	 * @return
	 */
	private ReqPAB4004 builder4004Req(BcdlMessage req)throws GopayException {
		PABOnlineHeaderMessage header = builderSDBOnlineHeaderMessage(PabConstants.ONLINE_TXN_TYPE_4004,((BisBcdlIntrConfig) req.getBankIntrChannConf()).getIntrMernum1());
		ReqPAB4004 pabReq = new ReqPAB4004();
		pabReq.setHeader(header);
		pabReq.setThirdVoucher(req.getOutOrderId());
		pabReq.setCstInnerFlowNo(req.getOutOrderId());// 客户自定义凭证号存放平台订单号
		pabReq.setCcyCode(pabConfig.get("PAB.BCDL.CCYCODE"));
		pabReq.setOutAcctNo(((BisBcdlIntrConfig) req.getBankIntrChannConf()).getAcctNum());
		pabReq.setOutAcctName(((BisBcdlIntrConfig) req.getBankIntrChannConf()).getAcctName());
		pabReq.setOutAcctAddr(pabConfig.get("PAB.BCDL.OUTACCTADDR"));
		pabReq.setInAcctBankNode(req.getPbocBankBranchCode());// 收款人开户行行号
		pabReq.setInAcctRecCode(req.getPbocBankBranchCode());// 接收行行号
		pabReq.setInAcctNo(req.getRecvBankAcctNum());// 收款人帐户
		pabReq.setInAcctName(req.getRecvBankAcctName());// 收款人帐户户名
		pabReq.setInAcctBankName(req.getPbocBankBranchName());// 收款人开户行名称
		pabReq.setInAcctProvinceCode(req.getRecvBankProvName());// 收款账户银行开户省代码或省名称
		pabReq.setInAcctCityName(req.getRecvBankCityName());// 收款账户开户市
		pabReq.setTranAmount(req.getAmount().toString());
		pabReq.setAmountCode("");
		pabReq.setUseEx("");
		pabReq.setUnionFlag(1 == req.getSysIOFlg() ? "0" : "1");
		pabReq.setSysFlag(1 == req.getEmergency() ? "Y" : "N");
		pabReq.setAddrFlag("" + req.getSysIOFlg());
		pabReq.setRealFlag("2");
		pabReq.setMainAcctNo("");
		pabReq.setMobile("");
		return pabReq;
	}

	/**
	 * 
	 * @param txnType
	 * @param tranDate
	 * @param tranTime
	 * @return
	 */
	private PABOnlineHeaderMessage builderSDBOnlineHeaderMessage(String txnType, String mercCode)throws GopayException {
		PABOnlineHeaderMessage header = new PABOnlineHeaderMessage();
		header.setMsgType(PabConstants.ONLINE_HEADER_MSG_TYPE);// 固定填A00101
		header.setMsgCharset(PabConstants.ONLINE_HEADER_MSG_CHARSET_U8);// UTF-8
		header.setProtocol(PabConstants.ONLINE_HEADER_PROTOCOL);// tcpip
		header.setMerchantCode(mercCode);// 企业银企直连编标准代码
		header.setMsgLength(StringUtils.rightPad("", 10, " "));// 报文体整体长度
		header.setTxnType(StringUtils.rightPad(txnType, 6, " "));//
		header.setUserOpid(StringUtils.rightPad("", 5, " "));// 操做员代码
		header.setServiceFlag(PabConstants.ONLINE_SERVICE_FLAG_REQ);// 服务类型
		String curr = DateUtils.getCurrDate();
		String tranDate = curr.substring(0, 8);
		String tranTime = curr.substring(8);
		header.setOrderDate(tranDate);// 交易日期
		header.setOrderTime(tranTime);// 交易时间
		header.setOrderId(StringUtils.rightPad(String.valueOf(System.currentTimeMillis()), 20, " "));// 请求方系统流水号
		header.setRetCd(StringUtils.rightPad("", 6, " "));// 返回码
		header.setRemark(StringUtils.rightPad("", 100, " "));// 返回描述
		header.setRepeatFlag("0");// 后续包标志，0-结束包，1-还有后续包
		header.setReqCount("000");// 请求次数
		header.setSignFlag(PabConstants.ONLINE_SIGN_FLAG_NO);// 签名标识
		header.setSignFormat(PabConstants.ONLINE_SIGN_FORMAT_1);// 签名数据包格式
		header.setSignAlgorithm(StringUtils.rightPad(PabConstants.ONLINE_SIGN_ALGORITHM, 12, " "));// 签名算法
		header.setSignLength(StringUtils.rightPad("", 10, "0"));// 签名数据长度
		header.setFileCount("0");
		return header;
	}
}
